enum List {
    Cons(i32, Box<List>),
    Nil,
}
use std::{
    ops::{Deref, DerefMut},
    rc::Rc,
};

use crate::List::{Cons, Nil};
fn main() {
    let arr = [1, 2, 3, 4];

    for val in arr {
        println!("{}", val)
    }

    // let iter = arr.iter();
    // println!("{:?}", iter.next());
    // println!("{:?}", iter.next());

    let total: i8 = arr.iter().sum();
    println!("{}", total);

    let arr = [1, 2, 3, 4];
    let arr2: Vec<_> = arr.iter().map(|val| val + 1).collect();

    for val in arr2 {
        println!("{}", val)
    }

    let b = Box::new(100);
    println!("{}", b);

    let list = Cons(1, Box::new(Cons(2, Box::new(Cons(3, Box::new(Nil))))));

    let s = String::from("hboot");
    // let s1 = &mut s;
    // *s1 += " admin";
    let mut s1 = Box::new(s);
    // *s1 += " admin";

    println!("{:?}", s1);

    let s = String::from("hboot");

    let s1 = MyBox::new(s);
    // *s1 += " admin";

    println!("{:?}", *s1);

    print(&s1);
    drop(s1);
    let s = MyBox::new(String::from("hboot"));

    let s1 = MyBox::new(s);
    // *s1 += " admin";

    println!("{:?}", s1);
    // Rc<T>
    let s = Rc::new(String::from("hboot"));
    println!("s create - {}", Rc::strong_count(&s));
    let s1 = Rc::clone(&s);
    println!("s1 create - {}", Rc::strong_count(&s));

    {
        let s2 = Rc::clone(&s);
        println!("s2 create - {}", Rc::strong_count(&s));
    }

    println!("s2 goes out of scope  - {}", Rc::strong_count(&s));
    // println!("s:{},s1:{},s2:{}", s, s1, s2)
}

#[derive(Debug)]
struct MyBox<T>(T);

impl<T> MyBox<T> {
    fn new(val: T) -> MyBox<T> {
        MyBox(val)
    }
}

impl<T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<T> Drop for MyBox<T> {
    fn drop(&mut self) {
        println!("mybox drop value");
    }
}

fn print(val: &str) {
    println!("{}", val)
}
